import threading
import time
import os
"""重新定义带返回值的线程类"""
class TimeOut(threading.Thread):
    def __init__(self,func,args=(),timeout=()):
        super(TimeOut,self).__init__()
        self.timeout=timeout
        self.func = func
        self.args = args
        self.setDaemon(True) 
        self.start()
        self.join(self.timeout)
        if self.is_alive() and self.callback:
            return threading.Timer(0, self.callback).start()  # 立即执行回调函数
        else:
            return 
        
    def run(self):
        self.result = self.func(*self.args)
    def get_result(self):
        try:
            return self.result
        except Exception:
            return None
    def callback(self):
        pass
    


def ping(x):
    print('hello')
    time.sleep(int(i))
    print('end')
    return x

for i in range(10):
    t=TimeOut(ping,str(i),timeout=0.1)

    print(t.get_result())

os._exit(0)
